from clusterer.clusterer import Clusterer
from sklearn.cluster import DBSCAN


# DBSCAN
class Dbscan(Clusterer):
    # 扫描半径
    eps = 0.5
    # 最小包含点数
    min_samples = 5
    # 噪声点
    uncluster_point_count = 0;
    
    def __init__(self):
        Clusterer.__init__(self)
        self.algorithm_name = "DBSCAN"
        self.ipynb_template_name = "dbscan-template.ipynb"
        
    def implent(self): 
        Clusterer.implent(self)
        # 构造模型
        self.algorithm = DBSCAN(eps=self.eps, min_samples=self.min_samples)
        # 分类
        self.algorithm.fit(self.inputs)
        # 结果
        self.label_list = self.algorithm.labels_
        self.cluster_count = len(set(self.label_list)) - (1 if -1 in self.label_list else 0)
        self.uncluster_point_count = 0
        for label in self.label_list:
            if label == -1:
                self.uncluster_point_count = self.uncluster_point_count + 1
        # 保存xlsx
        self.saveToExcle()
        # 绘图
        self.drawChart()
    
    def prepareIpynbItems(self):
        Clusterer.prepareIpynbItems(self)
        self.ipynb_items["#eps#"] = self.eps
        self.ipynb_items["#min_samples#"] = self.min_samples
